package cn.micro.system.manage.controller;


import cn.micro.system.manage.annotate.UniqueProduct;
import cn.micro.system.manage.common.log.annotation.Log;
import cn.micro.system.manage.common.log.enums.BusinessType;
import cn.micro.system.manage.common.result.Result;
import cn.micro.system.manage.common.security.utils.SecurityUtil;
import cn.micro.system.manage.common.vaildate.ValidateGroup;
import cn.micro.system.manage.entity.AssetDimProductInfo;
import cn.micro.system.manage.entity.dto.AssetDimProductSearch;
import cn.micro.system.manage.service.AssetDimProductInfoService;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 产品信息 前端控制器
 * </p>
 *
 * @author wr
 * @since 2024-01-05
 */
@RestController
@RequestMapping("/manage/assetDimProductInfo")
@RequiredArgsConstructor
@Validated
public class AssetDimProductInfoController {

    private final AssetDimProductInfoService assetDimProductInfoService;

    @GetMapping("{page}/{size}")
    @PreAuthorize("hasAuthority('bnt.assetProduct.list')")
    public Object list(@PathVariable int page, @PathVariable int size, AssetDimProductSearch search){
        return Result.success(new PageInfo<>(assetDimProductInfoService.listByPage(page, size, search)));
    }

    @GetMapping("detail/{id}")
    @PreAuthorize("hasAuthority('bnt.assetProduct.list')")
    public Object detail(@PathVariable Long id){
        return Result.success(assetDimProductInfoService.detail(id));
    }

    @PostMapping("save")
    @Log(title = "项目管理--新增项目信息表", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.assetProduct.save')")
    public Object save(@Validated(value = ValidateGroup.add.class) @UniqueProduct @RequestBody AssetDimProductInfo assetDimProductInfo){
        Long userId = SecurityUtil.getUserId();
        assetDimProductInfo.setCreateUserId(userId);
        assetDimProductInfo.setUpdateBy(userId);
        if (!assetDimProductInfoService.save(assetDimProductInfo)){
            return Result.fail();
        }
        return Result.success();
    }

    @PutMapping("edit")
    @Log(title = "项目管理--修改项目信息表", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.assetProduct.edit')")
    public Object edit(@Validated(value = ValidateGroup.add.class) @UniqueProduct @RequestBody AssetDimProductInfo assetDimProductInfo){
        assetDimProductInfo.setUpdateBy(SecurityUtil.getUserId());
        if (!assetDimProductInfoService.updateById(assetDimProductInfo)){
            return Result.fail();
        }
        return Result.success();
    }

    @DeleteMapping("delete/{id}")
    @Log(title = "项目管理--删除项目信息表", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.assetProduct.remove')")
    public Object delete(@PathVariable Long id){
        AssetDimProductInfo productInfo = new AssetDimProductInfo();
        productInfo.setId(String.valueOf(id));
        productInfo.setUpdateBy(SecurityUtil.getUserId());
        productInfo.setIsDeleted(1);
        if (!assetDimProductInfoService.updateById(productInfo)){
            return Result.fail();
        }
        return Result.success();
    }

}
